<?php

namespace App\Repositories;

use EasyWeChat\Foundation\Application;
use Illuminate\Container\Container as App;

class WechatRepository{

    protected $easyOptions;
    protected $app;
    protected $server;
    public $adminArray = [];
    protected $userService;


    public function __construct()
    {
        $this->adminArray = [
          'oryQC0WNgdX8mMa_5nVKhkHaFlWM',//欢
          'o1M5awbK1ugql5bfZjMUuxn7mg4U',//祝总
//          'o1M5awUSgfQAomHvivlrAFsGm9Jg',//z
          'o1M5awb8x6-YeYPntu16dNxwivZ0',//zh
          'o1M5aweZ10LCa4Gn2nzl61Ex0lQU',//豌豆
          'o1M5awY5VOmJgj6H75ZCsHxTbCp0',//梵音
        ];
        $this->easyOptions = [
            /**
             * Debug 模式，bool 值：true/false
             *
             * 当值为 false 时，所有的日志都不会记录
             */
            'debug'  => true,
            /**
             * 账号基本信息，请从微信公众平台/开放平台获取
             */
            'app_id'  => 'wxf75256ab7b42cc66',         // AppID
            'secret'  => '270561d3e2d02b953d64618e47f919b1',     // AppSecret
            'token'   => 'asasas',          // Token
            'aes_key' => '25WqrFXTyuARa6LvEVHcpClbLRNLUYhCwoh7T59B7Fq',                    // EncodingAESKey，安全模式下请一定要填写！！！
            /**
             * 日志配置
             *
             * level: 日志级别, 可选为：
             *         debug/info/notice/warning/error/critical/alert/emergency
             * permission：日志文件权限(可选)，默认为null（若为null值,monolog会取0644）
             * file：日志文件位置(绝对路径!!!)，要求可写权限
             */
            'log' => [
                'level'      => 'debug',
                'permission' => 0777,
                'file'       => '/tmp/easywechat.log',
            ],
            /**
             * OAuth 配置
             *
             * scopes：公众平台（snsapi_userinfo / snsapi_base），开放平台：snsapi_login
             * callback：OAuth授权完成后的回调页地址
             */
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/examples/oauth_callback.php',
            ],
            /**
             * 微信支付
             */
            'payment' => [
                'merchant_id'        => 'your-mch-id',
                'key'                => 'key-for-signature',
                'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径！！！！
                'key_path'           => 'path/to/your/key',      // XXX: 绝对路径！！！！
                // 'device_info'     => '013467007045764',
                // 'sub_app_id'      => '',
                // 'sub_merchant_id' => '',
                // ...
            ],
            /**
             * Guzzle 全局设置
             *
             * 更多请参考： http://docs.guzzlephp.org/en/latest/request-options.html
             */
            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
                //'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
            ],
        ];
        $this->app = new Application($this->easyOptions);

        $this->server = $this->app->server;

        $this->userService = $this->app->user;
    }

    public function sendTempMsg($openId,$templateId,$url,$array)
    {
        $data = [
            'touser' => $openId,
            'template_id' => $templateId,
            'url' => $url,
            'data' => $this->setColor($array),
        ];
        if($openId){
            try
            {
                $res = $this->app->notice->send($data);
            }
            catch(\Exception $e)
            {
                $res =  ['errcode'=>0,'errmsg'=>$e->getMessage()];
            }
        }else{
            $res =  ['errcode'=>0,'errmsg'=>'not find user openid recored'];
        }

        if($res['errcode']==0){
            return ['status'=>1,'msg'=>'ok'];
        }else{
            return ['status'=>0,'msg'=>$res['errmsg']];
        }
    }

    protected function setColor($data)
    {
        $res = [];
        if($data){
            foreach ($data as $k=>$v) {
                $res[$k] = [$v,'#000000'];
            }
        }
        return $res;
    }

    public function dumpUser()
    {
        $list = $this->userService->lists();
        dd($list);
    }

}